/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daos;

import beans.Bean;
import beans.ObraSocialBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Milene
 */
public class MSSQLObraSocialDao extends MSSQLDao implements Dao{

    public MSSQLObraSocialDao() throws Exception {
        
        super();
    }         


    @Override
    public Bean make(ResultSet result) throws Exception {
       
        ObraSocialBean osb = new ObraSocialBean();
        osb.setNombreOS(result.getString("nombre"));
        osb.setDireccionOS(result.getString("direccion_os"));
        osb.setPaginaWebOS(result.getString("pagina_web_os"));
        osb.setEmailOS(result.getString("email_os"));
        osb.setHorarioAtencionOS(result.getString("horario_atencion_os"));
        osb.setHorarioContactoOS(result.getString("horario_contacto_os"));
        osb.setNombreContactoOS(result.getString("nombre_contacto_os"));
        osb.setEmailContactoOS(result.getString("email_contacto_os"));
        osb.setAccesoWSOS(result.getString("acceso_ws_os"));
        osb.setTelefonoOS(result.getString("telefono"));
        osb.setBarrio(result.getString("barrio"));
        osb.setCiudad(result.getString("ciudad"));
        
        return osb;
    }

    @Override
    public int insert(Bean bean) throws Exception {
       
         try
        {
        this.getConnection().setAutoCommit(false);
        this.setStatement("insertar_obrasoacial(?,?,?,?,?,?,?,?,?,?,?,?)");
        this.getStatement().setString(1, ((ObraSocialBean) bean).getNombreOS());
        this.getStatement().setString(2, ((ObraSocialBean) bean).getDireccionOS());
        this.getStatement().setString(3, ((ObraSocialBean) bean).getPaginaWebOS());
        this.getStatement().setString(4, ((ObraSocialBean) bean).getEmailOS());
        this.getStatement().setString(5, ((ObraSocialBean) bean).getHorarioAtencionOS());
        this.getStatement().setString(6, ((ObraSocialBean) bean).getNombreContactoOS());
        this.getStatement().setString(7, ((ObraSocialBean) bean).getEmailContactoOS());
        this.getStatement().setString(8, ((ObraSocialBean) bean).getHorarioContactoOS());
        this.getStatement().setString(9, ((ObraSocialBean) bean).getAccesoWSOS());
        this.getStatement().setString(10, ((ObraSocialBean) bean).getTelefonoOS());
        this.getStatement().setString(11, ((ObraSocialBean) bean).getBarrio());
        this.getStatement().setString(12, ((ObraSocialBean) bean).getCiudad());
        this.getStatement().executeUpdate();

        } catch (SQLException ex) {
            this.getConnection().rollback();
            throw ex;
        } catch (Exception ex2) {
            this.getConnection().rollback();
            throw ex2;
        } finally {
            this.getConnection().setAutoCommit(true);
        }
        return 0;
    }

 
    @Override
    public int delete(Bean bean) throws Exception {
        int rs = 0;
        try {
            this.getConnection().setAutoCommit(false);//para mantener la integridad de la base, usar transaccion

            this.setStatement("eliminar_obrasocial(?)");
            this.getStatement().setString(1, ((ObraSocialBean) bean).getNombreOS());

            this.getStatement().executeUpdate();
            
            this.getConnection().commit();

            
        } catch (SQLException ex) {
            this.getConnection().rollback();
            throw ex;
        } catch (Exception ex2) {
            this.getConnection().rollback();
            throw ex2;
        } finally {
            this.getConnection().setAutoCommit(true);
        }
        return rs;

    }

    @Override
    public List<Bean> select(Bean bean) throws Exception {
        
        ResultSet result;
        List<Bean> os=new ArrayList<Bean>();
        this.setStatement("get_obra_social");
        result=this.getStatement().executeQuery();
        while(result.next()){
            
            os.add(this.make(result));
        }
        
        return os;
    }


    @Override
    public boolean valid(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Bean get(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public long getSize(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int update(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

  
}
